// pages/bindingBank/bindingBank.js
var app = getApp();
var model = require('../../utils/model/model.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    modelData: {
      showModal: false,
      signle: true,
      signleBtnTxt: "确定",
      content: '',
    },
    formlist: [
      { label: '请绑定您的储蓄卡', type: 'label' },
      { isrequire: false, label: '持卡人', key: 'cardHolder', valueType: 'text', tips: '请输入持卡人姓名', type: 'input'},
      { isrequire: false, label: '卡号', key: 'cardNumber', valueType: 'number', tips: '请输入银行卡号', type: 'input' },
      { isrequire: false, label: '开户省份', key: 'provinceCityNo', valueType: 'text', tips: '请选择银行卡开户省份', type: 'select', data: [], rangeKey:'provinceName' },
      { isrequire: false, label: '开户行', key: 'bankNo', valueType: 'text', tips: '请选择银行卡开户行', type: 'select', data: [], rangeKey: 'bankName' },
      { isrequire: false, label: '手机号', key: 'phone', valueType: 'number', tips: '请输入银行柜台预留手机号', type: 'input', reg: "^1[34578][0-9]{9}$"  }
    ],
    formData: {
      userId:'',
      cardHolder: "",
      cardNumber: '',
      bankNo: '',
      phone: '',
      provinceCityNo:''
    },
    showData:{
      provinceCityNo:'',
      bankNo:''
    },
    currentInput: {},
    canSubmit: false,
    delta: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    this.setData({
      'formData.userId': this.data.userInfo.id,
      delta: options.delta
    })
    model.init.apply(this, ['modelData']);
    this.getProvinceCity();
    this.getBank();
  },
  getBank:function(e){
    var that=this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyBank/all', //仅为示例，并非真实的接口地址
      data: {},
      success: function (res) {
        if (res.data.success) {
          that.setData({
            ['formlist[4].data']: res.data.data
          })
        }
      }
    })
  },
  getProvinceCity: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyProvinceCity/all', //仅为示例，并非真实的接口地址
      data: {},
      success: function (res) {
        if (res.data.success) {
          that.setData({
            ['formlist[3].data']: res.data.data
          })
        }
      }
    })
  },
  bindPickerChange:function(e){
    var item = e.currentTarget.dataset.item;
    this.setData({
      ['formData.' + item.key]: item.data[e.detail.value].id,
      ['showData.' + item.key]: item.data[e.detail.value][item.rangeKey],
    })
    this.changeValue();
  },
  changeTextarea: function (e) {//输入框值改变事件
    var key = e.target.dataset.value;
    this.data.formData[key] = e.detail.value;
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  checkReg: function (data) {
    var flag = true;
    for (var i = 0; i < data.length; i++) {
      if (data[i].reg) {
        var reg = new RegExp(data[i].reg);
        if (!reg.test(this.data.formData[data[i].key])) {
          flag = false;
          this.setData({
            'modelData.showModal':true,
            'modelData.content': '请输入正确的' + data[i].label,
          })
          return;
        }
      }
    }
    return flag;
  },
  saveCarInfo: function (e) {
    var that=this;
    if (this.data.canSubmit) {
      if (this.checkReg(this.data.formlist)){
        wx.request({
          url: app.globalData.httpApiCar + 'tGyBankCard/save', //仅为示例，并非真实的接口地址
          data: this.data.formData,
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {
            if (res.data.success) {
              wx.redirectTo({
                url: '/pages/regsuccess/regsuccess?btnText=返回我的钱包&showText=绑定成功&delta=1'
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '绑定失败',
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                  }
                }
              })
            }
          }
        })
      }
    }
  },
  changeValue() {
    var flag = true;
    var item = this.data.formData;
    if ((item.cardHolder == '') || (item.cardNumber == '') || (item.bankNo == '') || (item.phone == '') || (item.provinceCityNo == '')) {
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})